package com.lovo.middleman.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.middleman.bean.BuyCarBean;
import com.lovo.middleman.mapper.BuyCarMapper;
import com.lovo.middleman.service.IBuyCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BuyCarServiceImpl extends ServiceImpl<BuyCarMapper, BuyCarBean> implements IBuyCarService {

    @Autowired
    BuyCarMapper buyCarMapper;

    /**
     * 根据用户id 查询购物车
     * @param userId
     * @return
     */
    @Override
    public List<BuyCarBean> queryAllBuyCarBeanByUserId(int userId) {
        QueryWrapper<BuyCarBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        return buyCarMapper.selectList(queryWrapper);
    }



//    @Override
//    public BuyCarBean queryAllBuyCarBean() {
//        return null;
//    }

    @Override
    public void addBuyCarBean(BuyCarBean buyCarBean,int userId) {
        QueryWrapper<BuyCarBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<BuyCarBean> buyCarBeans = buyCarMapper.selectList(queryWrapper);
        for (BuyCarBean carBean : buyCarBeans) {
            if (!carBean.getGoodsName().equals(buyCarBean.getGoodsName())){
                buyCarMapper.insert(buyCarBean);
            }else {
                //修改
                carBean.setBuyNumber(buyCarBean.getBuyNumber());
               buyCarMapper.updateById(carBean);
            }
        }
    }

    @Override
    public void updateBuyCarBean(BuyCarBean buyCarBean) {
        buyCarMapper.updateById(buyCarBean);
    }

    @Override
    public void deleteBuyCarBean(int carId) {
        buyCarMapper.deleteById(carId);
    }

}
